package cn.dotalk.usermanage.learn;

import cn.dotalk.usermanage.entity.CarEntity;
import cn.dotalk.usermanage.entity.PersonEntity;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DtoJson {

    public static void main(String[] args) {
        // dto => json
        PersonEntity personEntity = new PersonEntity(22, "hi cat.");
        System.out.println(personEntity);
        System.out.println(JSON.toJSON(personEntity));

        // json dto   =>   利用   google   Gson依赖
        // {"name":"别克","level":"1级.","price":5.66}
        CarEntity carEntity0 = new CarEntity();
        carEntity0.setName("别克");
        carEntity0.setLevel("1级");
        carEntity0.setPrice(5.68);
        Object carJson = JSON.toJSON(carEntity0);
        System.out.println(carJson);

        CarEntity carEntity = new Gson().fromJson(carJson.toString(), CarEntity.class);
        System.out.println(carEntity.getLevel());


        // List<T>
        int[] arr = {1, 23};
        System.out.println(arr[0]);
        //数组地址
        System.out.println(arr);
        System.out.println(arr.toString());

        //数组定义并赋值
        String[] arr1 = {"y", "o", "n", "g"};
        System.out.println(arr1[3]);

        List<String> list = new ArrayList<>();
        list.add("y");
        list.add("o");
        System.out.println(list.get(1));


        //json => List<T>
        List<CarEntity> catList = new ArrayList<>();
        catList.add(carEntity0);
        catList.add(carEntity0);
        catList.add(carEntity0);
        try {
            Map<String, List<CarEntity>> data = new HashMap<>();
            data.put("data", catList);
            JSONObject json = JSONObject.parseObject(JSON.toJSON(data).toString());
            System.out.println(json);
            JSONArray array = json.getJSONArray("data");
            System.out.println(array);
            List<CarEntity> carEntities = new ArrayList<>();
            for (int i = 0; i < array.size(); i++) {
                JSONObject jo = array.getJSONObject(i);
                System.out.println(jo);
                // break;
                carEntity = new CarEntity();
                carEntity.setLevel(jo.getString("level"));
                carEntity.setName(jo.getString("name"));
                carEntity.setPrice(jo.getDouble("price"));
                carEntities.add(carEntity);
            }

            System.out.println(carEntities);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // JsonParser parser = new JsonParser();
        // JsonArray jsonArray = parser.parse(JSON.toJSON(catList).toString()).getAsJsonArray();
        // ArrayList<T> carBeanList = new ArrayList<T>();
        // for (carEntity car : jsonArray) {
        //     carEntity carBean = new Gson().fromJson(car, carEntity.class);
        //     carBeanList.add(carBean);
        // }
        // System.out.println(carEntity);

        // List<CarEntity> carEntitys = new Gson().fromJson(JSON.toJSON(catList).toString(), new TypeToken<List<CarEntity>>(){}.getType());
        // System.out.println(carEntitys);

    }
}
